package com.wpf.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpf.system.entity.*;
import com.wpf.system.entity.vo.PointJournalVO;
import com.wpf.system.service.*;
import com.wpf.system.mapper.PointJournalMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author ferna
 * @description 针对表【point_journal(积分明细表)】的数据库操作Service实现
 * @createDate 2023-02-06 10:43:54
 */
@Service
public class PointJournalServiceImpl extends ServiceImpl<PointJournalMapper, PointJournal> implements PointJournalService{

    @Resource
    private AssetService assetService;

    @Resource
    private FieldBookService fieldBookService;

    @Resource
    private EquipmentRentService equipmentRentService;

    @Resource
    private EquipmentBuyService equipmentBuyService;

    public void pointOut(Orders orders){
        Asset asset = assetService.getAssetByUid(orders.getUid());
        PointJournal pointJournal = new PointJournal();
        pointJournal.setAid(asset.getId());
        pointJournal.setOid(orders.getId());
        pointJournal.setType(1);
        pointJournal.setRemark(orders.getType());
        pointJournal.setPoint(asset.getPoint()-orders.getPoint());
        pointJournal.setPointChange(orders.getPoint());
        asset.setPoint(asset.getPoint()-orders.getPoint());
        this.baseMapper.insert(pointJournal);
        assetService.updateById(asset);
    }

    @Override
    public void pointInto(Orders orders, int remark) {
        Asset asset = assetService.getAssetByUid(orders.getUid());
        PointJournal pointJournal = new PointJournal();
        pointJournal.setAid(asset.getId());
        pointJournal.setOid(orders.getId());
        pointJournal.setType(0);
        pointJournal.setRemark(remark);

        Long BackPoint = 0L;
        Long num;
        switch (remark){
            //退还一半积分/子订单个数
            case 0:
                //预约积分退还
                LambdaQueryWrapper<FieldBook> queryWrapper0 = new LambdaQueryWrapper<>();
                queryWrapper0.eq(FieldBook::getOid,orders.getId());
                num = fieldBookService.getBaseMapper().selectCount(queryWrapper0);
                BackPoint = Convert.toLong(Math.ceil(orders.getPoint()*0.5)/num);
                break;
            case 1:
                //器材租赁积分退还
                LambdaQueryWrapper<EquipmentRent> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(EquipmentRent::getOid,orders.getId());
                num = equipmentRentService.getBaseMapper().selectCount(queryWrapper1);
                BackPoint = Convert.toLong(Math.ceil(orders.getPoint()*0.5)/num);
                break;
            case 2:
                //器材购买积分退还
                LambdaQueryWrapper<EquipmentBuy> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(EquipmentBuy::getOid,orders.getId());
                num = equipmentBuyService.getBaseMapper().selectCount(queryWrapper2);
                BackPoint = Convert.toLong(Math.ceil(orders.getPoint()*0.5)/num);
                break;
            case 3:
                //消费所得积分
                BackPoint = Convert.toLong(orders.getCost().multiply(BigDecimal.valueOf(100)));
                break;
        }

        pointJournal.setPointChange(BackPoint);
        pointJournal.setPoint(asset.getPoint()+BackPoint);
        asset.setPoint(asset.getPoint()+BackPoint);
        this.baseMapper.insert(pointJournal);
        assetService.updateById(asset);
    }

    @Override
    public List<PointJournalVO> getPointJournal(Long aid, String type, String startTime, String endTime, Integer page, Integer pageSize) {
        return this.baseMapper.getPointJournal(aid,type,startTime,endTime,page,pageSize);
    }
}